package cc.telecomdigital.tdstock.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.activity.groups.stock.WarrantCBBCActivity;

public class WarrantCBBCAdapter extends BaseAdapter
{
	private LayoutInflater			minflater;
	private List<TwoTextColumnHolder>	list;

	/*	private final int[]  RESOURCE_ARRAY_STRING_IDS = {
					R.array.stock_wc_type,
					R.array.stock_wc_put_call_warrant,
					R.array.stock_wc_warrant_date_due,
					R.array.stock_wc_warrant_price,
					R.array.stock_wc_premium_price,
					R.array.stock_wc_total_process
				};
		
		private String[][] array_string_resourceId ;*/
	public WarrantCBBCAdapter(LayoutInflater inflater,
			List<TwoTextColumnHolder> list)
	{
		super();
		this.minflater = inflater;
		this.list = list;
		/*array_string_resourceId = new String[6][];
		for(int index = 0; index < RESOURCE_ARRAY_STRING_IDS.length; index++) {
			String[] res = inflater.getContext().getResources().getStringArray(RESOURCE_ARRAY_STRING_IDS[index]);
			array_string_resourceId[index] = new String[res.length];
			array_string_resourceId[index] = res;
		}*/
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position < this.list.size() ? this.list.get(position) : null;
	}

	@Override
	public long getItemId(int position)
	{
		return position < this.list.size() ? this.list.get(position).id : -1l;
	}

	/*@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TwoTextColumnHolder commHolder = null;
		   if(convertView == null) {
			    //convertView = minflater.inflate(R.layout.moreitem, null);
			   convertView = minflater.inflate(R.layout.two_text_column_item, null);
			    commHolder = new TwoTextColumnHolder();
			    commHolder.left= (TextView)convertView.findViewById(R.id.more_item_left);
			    commHolder.right = (TextView)convertView.findViewById(R.id.more_item_right);
			    convertView.setTag(commHolder);
		   }else {
			   commHolder =  (TwoTextColumnHolder) convertView.getTag();
		   }
		   if(this.list!= null && !this.list.isEmpty()) {
			   commHolder.left.setText(this.list.get(position).left.getText());
			   commHolder.right.setText(this.list.get(position).right.getText());
		   }
		   return convertView;
		
		
		
		 OptionsView sv;
	    if (convertView == null) {
	         sv = new OptionsView(minflater.getContext() ,position, mExpanded[position]);
	         convertView.setTag(sv);
	     } else {
	         sv = (OptionsView)convertView.getTag();
	//              sv.setExpanded(mExpanded[position]);
	//              sv.setColumn(position);
	     }
	     sv.setColumn(position);
	     return sv;
		
		
		
		
		
		
	}*/

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		TwoTextColumnHolder commHolder = null;
		if (convertView == null)
		{
			//convertView = minflater.inflate(R.layout.moreitem, null);
			convertView = minflater.inflate(R.layout.stock_warrant_item,
					null);
			commHolder = new TwoTextColumnHolder();
			commHolder.left = (TextView) convertView
					.findViewById(R.id.more_item_left);
			commHolder.right = (TextView) convertView
					.findViewById(R.id.more_item_right);

			//synchronize with iphone
			commHolder.left.setTextColor(Color.BLACK);
			commHolder.right.setTextColor(Color.BLACK);
			convertView.setDrawingCacheBackgroundColor(Color.WHITE);

			//synchronize with iphone

			convertView.setTag(commHolder);
			WarrantCBBCActivity.cViews.add(commHolder);
		}
		else
		{
			commHolder = (TwoTextColumnHolder) convertView.getTag();
		}
		if (this.list != null && !this.list.isEmpty())
		{
			commHolder.left.setText(this.list.get(position).left.getText());
			commHolder.right
					.setText(this.list.get(position).right.getText());
		}
		return convertView;
	}
	
	/*
	  private boolean[] mExpanded = 
	 {
	         false,
	         false,
	         false,
	         false,
	         false,
	         false
	 };
	  
	  public void toggle(int position) {
	     mExpanded[position] = !mExpanded[position];
	     notifyDataSetChanged();
	 }
	  
	  
	  private class OptionsView extends RelativeLayout {
		  //private RadioGroup mradioGp;
		  private TextView mleft;
		  private TextView mright;
		  
	        public OptionsView(Context context,  int position, boolean expanded) {
	            super(context);
	            
	            RelativeLayout  line = new  RelativeLayout(context);
	            TwoTextColumnHolder holder = list.get(position);
	            mleft = holder.left;
	            mright = holder.right;
	             {
	            	 LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 24);
	            	 params.addRule( RelativeLayout.ALIGN_PARENT_LEFT);
		        	  params.alignWithParent = true;
		             line.addView(mleft, params);
	             }
	             {
	            	 LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 24);
	            	 params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT);
		        	  params.alignWithParent = true;
		             line.addView(mright, params);
	             }
	             addView(line);
	             
	            mradioGp = new RadioGroup(context);
	             String[] items = array_string_resourceId[position];
	             for(String item: items) {
	            	 RadioButton  rBtn = new RadioButton(context);
	            	 rBtn.setWidth(LayoutParams.WRAP_CONTENT);
	            	 rBtn.setHeight(LayoutParams.WRAP_CONTENT);
	            	 rBtn.setText(item);
	            	 rBtn.setChecked(false);
	            	 rBtn.setOnCheckedChangeListener( new  OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							// TODO Auto-generated method stub
							toggle(position);
						}
	            		 
	            	 });
	            	 mradioGp.addView(rBtn, new RelativeLayout.LayoutParams(120, 30));
	             }
	             {
	            	 LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	            	 params.addRule( RelativeLayout.CENTER_IN_PARENT);
		        	 params.alignWithParent = true;
		        	 addView(mradioGp, params);
	             }
	             
	             setExpanded(expanded );
	        }
	        


	        public void setExpanded(boolean expanded) {
	        	mradioGp.setVisibility(expanded ? VISIBLE : GONE);
	        }
	        
	        public void setColumn(int position) {
	        	TwoTextColumnHolder holder = list.get(position);
	        	if(holder == null) return;
	        	if(mleft == null) {
	        		mleft = holder.left;
	        	}
	        	if(mright == null) {
	        		mright = holder.right;
	        	}
	        	mleft.setText(holder.left.getText());
	        	mright.setText(holder.right.getText());
	        }
	        

	    }*/

}
